package com.china08.yxyapi.api.comm.sws;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSection;
import com.china08.yxyapi.entity.mg.sws.SwsArticle;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.SwsSectionRepository;
import com.china08.yxyapi.repository.mg.SwsArticleRepository;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = { "V01", "网站栏目修改" })
@CrossOrigin
@RestController
public class SwsSectionArticleController extends YxyApiBaseController {

	@Autowired
	private SwsSectionRepository ssr;

	@Autowired
	private SwsArticleRepository sarr;

	@ApiOperation(value = "修改外联栏目下的文章,将文章的栏目地址更新为空")
	@RequestMapping(value = "/api_yw/comm/sws/section", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public void execute(@PathVariable(value = "name", required = false) String name) {

		// 查询外联的栏目
		List<SwsSection> ss = ssr.findByIsOutLinkOrderByOrderNumAsc(true);
		if (CollectionUtils.isNotEmpty(ss)) {
			ss.forEach(section -> {
				List<SwsArticle> lsa1 = sarr.findBySection1Id(section.getId());
				if (CollectionUtils.isNotEmpty(lsa1)) {
					lsa1.forEach(article -> {
						article.setSection1Id("");
						article.setSection2Id("");
					});
				}
				sarr.save(lsa1);
				List<SwsArticle> lsa2 = sarr.findBySection2Id(section.getId());
				if (CollectionUtils.isNotEmpty(lsa2)) {
					lsa2.forEach(article -> {
						article.setSection1Id("");
						article.setSection2Id("");
					});
				}
				sarr.save(lsa2);
			});
		}

	}

	protected static class ReqModel extends YxyApiReqModel {

	}

	protected static class RespModel {

	}

}
